<template>
  <div>
    <el-container class="add-contianer">
      <el-button
        @click="hadleAddRole"
        icon="el-icon-circle-plus-outline"
        v-if="isPermissionsBtn('role_add')"
        type="primary"
      >新增角色</el-button>
    </el-container>
    <div class="table-body">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" width="50" label="序号"></el-table-column>
        <el-table-column prop="roleName" width="200" label="角色名称"></el-table-column>
        <el-table-column prop="remark" label="角色说明"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleClickEdit(scope.row)"
              type="success"
              v-if="isPermissionsBtn('role_update')"
              size="small"
            >编辑</el-button>
            <el-popconfirm title="确定删除此角色？" @onConfirm="handleClickDelete(scope.row)">
              <el-button
                style="margin-left:15px"
                size="small"
                type="danger"
                v-if="isPermissionsBtn('role_del')"
                slot="reference"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
      <el-dialog title="新增角色" :visible.sync="isVisible" width="50%">
        <add-Role @submit="getMag" v-if="isVisible" />
      </el-dialog>
      <el-dialog title="修改角色信息" :visible.sync="isVisible1" width="50%">
        <UpdateRole @submit="changeInfo" :data="propdata" v-if="isVisible1" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AddRole from "./addRole";
import UpdateRole from "./UpdateRole";
import { mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      tableData: [],
      total: 0,
      isVisible: false,
      isVisible1: false,
      param: {
        queryRequest: {
          pageSize: 8,
          pageNum: 1
        }
      },
      propdata: {}
    };
  },
  components: {
    "add-Role": AddRole,
    UpdateRole
  },
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
  },
  methods: {
    //列表数据
    initData() {
      let data = this.param.queryRequest;
      this.$service.getRoleLists(data).then(res => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
      });
    },
    //新增角色
    hadleAddRole() {
      this.isVisible = true;
    },
    //编辑角色
    handleClickEdit(scope) {
      this.isVisible1 = true;
      this.propdata = JSON.parse(JSON.stringify(scope));
    },
    //删除角色
    handleClickDelete(scope) {
      this.$service.RoleDelete(scope.roleId).then(res => {
        if (res.code == "200") {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    currentPageChange(e) {
      this.param.queryRequest.pageNum = e;
      this.initData();
    },
    //?
    getMag(message) {
      this.isVisible = false;
      this.initData();
    },
    //?
    changeInfo() {
      this.isVisible1 = false;
      this.initData();
    }
  },
  watch: {}
};
</script>
<style scoped>
</style>
